//
//  QianBaoChongViewController.m
//  DBAChain
//
//  Created by Evan on 2018/9/9.
//  Copyright © 2018 Evan. All rights reserved.
//

#import "QianBaoChongViewController.h"
#import "CommonData.h"
#import "taskOverviewService.h"
#import "SGQRCodeTool.h"
#import "dataCodeInfo.h"

@interface QianBaoChongViewController ()
@property (weak, nonatomic) IBOutlet UILabel *lbcode;
@property (weak, nonatomic) IBOutlet UIImageView *codeImage;
@property (weak, nonatomic) IBOutlet UILabel *lbcontent;
@property (weak, nonatomic) IBOutlet UIButton *imageButton;
@end

@implementation QianBaoChongViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = self.isGTE ? @"GTE充币":@"GTK充币";
    [self initUI];
    self.topBgView.hidden = NO;
    if (self.isGTE) {
        [self loadData];
    }else{
        self.topBgView.hidden = YES;
        self.lbcode.text = [UserCenter shareCenter].user.userNo;
    }
}

-(void)initUI{
    self.lbcontent.text = self.isGTE ? @"请勿向上述地址充值任何非GTE资产，否则资产将不可找回。您充值至上述地址后，需要整个网络节点的确认。" :@"请勿向上述地址充值任何非GTK资产，否则资产将不可找回。您充值至上述地址后，需要整个网络节点的确认。";
    self.codeImage.hidden = !self.isGTE;
    self.imageButton.hidden = !self.isGTE;
    self.topBgView.layer.shadowColor = [UIColor blackColor].CGColor;
    //        阴影的透明度
    self.topBgView.layer.shadowOpacity = 0.2f;
    self.topBgView.layer.shadowOffset = CGSizeMake(0, 0 );
    //        阴影的圆角
    self.topBgView.layer.shadowRadius = 4.0f;
}

-(void)loadData{
    [taskOverviewService getRecharge:nil success:^(dataCodeInfo *response, BOOL success) {
        if (success) {
            self.lbcode.text = response.data;
            self.codeImage.image = [SGQRCodeTool SG_generateWithDefaultQRCodeData:response.data imageViewWidth:204];
        }
    } failture:^(NSError *error, NSString *strDescription) {
        
    }];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self setBlackStatus:YES];
    
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:LCFromRGB(51, 51, 51), NSForegroundColorAttributeName, nil]];
    if (self.navigationController.navigationBar.hidden == YES) {
        self.navigationController.navigationBar.hidden = NO;
    }
}

- (IBAction)copyAction:(id)sender {
    UIPasteboard *Pasteboard = [UIPasteboard generalPasteboard];
    [Pasteboard setString:self.lbcode.text];
    if (Pasteboard == nil) {
        [DKProgressHUD showErrorWithStatus:@"复制失败"];
    }else{
        [DKProgressHUD showSuccessWithStatus:@"复制成功"];
    }
}

- (IBAction)saveImageAction:(id)sender {
    [self loadImageFinished:self.codeImage.image];
}

- (void)loadImageFinished:(UIImage *)image
{
    UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), (__bridge void *)self);
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    if(error){
        [DKProgressHUD showErrorWithStatus:@"保存图片失败"];
    }else{
        [DKProgressHUD showErrorWithStatus:@"保存图片成功"];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
